package com.yaoyong.yy_demo.module.rabbitmq.springboot;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

import static com.yaoyong.yy_demo.config.RabbitMQConfig.DELAY_EXCHANGE;
import static com.yaoyong.yy_demo.config.RabbitMQConfig.TOPIC_EXCHANGE;

@Slf4j
@RestController
@RequestMapping(value = "rabbitmq")
@Tag(name = "rabbitmq演示controller", description = "手动控制消息发送时机,查看消息接收情况")
public class Producer {

    @Autowired
    public RabbitTemplate rabbitTemplate;

    private void sendDelayMsg(String exchange, String routingKey, Object message, Integer delayTime) {
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend(exchange, routingKey, message, a -> {
            a.getMessageProperties().setDelay(delayTime);
            return a;
        }, correlationData);
        log.info("发送了消息---" + message + "---消息ID---" + correlationData.getId());
    }

    @GetMapping("sendMsg")
    @Operation(summary = "发送消息接口", description = "默认发送消息到springboot_topic_exchange交换机")
    @Parameter(name = "msg", description = "发送的消息", example = "this is a real time message")
    @Parameter(name = "routingKey", description = "消息的路由规则", example = "springboot.key1")
    public ResponseEntity<String> sendMsg(@RequestParam(name = "msg", defaultValue = "this is a real time message") String msg,
                                          @RequestParam(name = "routingKey", defaultValue = "") String routingKey) {
        try {
            this.sendDelayMsg(TOPIC_EXCHANGE, routingKey, msg, 0);
            return ResponseEntity.ok(msg);
        } catch (AmqpException e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    @GetMapping("sendTTLMsg")
    @Operation(summary = "发送延迟消息接口", description = "默认发送消息到springboot_delay_exchange交换机")
    @Parameter(name = "msg", description = "发送的消息", example = "this is a delay message")
    @Parameter(name = "delayTime", description = "消息延迟多长时间(毫秒)发送", example = "10000")
    @Parameter(name = "routingKey", description = "消息的路由规则", example = "springboot_delay.key1")
    public ResponseEntity<String> sendTTLMsg(@RequestParam(name = "msg", defaultValue = "this is a delay message") String msg,
                                             @RequestParam(name = "delayTime", defaultValue = "5000") Integer delayTime,
                                             @RequestParam(name = "routingKey", defaultValue = "springboot_delay.") String routingKey) {
        try {
            this.sendDelayMsg(DELAY_EXCHANGE, routingKey, msg, delayTime);
            return ResponseEntity.ok(msg);
        } catch (AmqpException e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

}
